<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxDistance = function (nums1, nums2) {
      let len1 = nums1.length;
      let len2 = nums2.length;
      let L = 0;
      let R = 0;

      while (L < len1 && R < len2) {
        if (nums1[L] > nums2[R]) {
          L++;
        }
        R++;
      }

      return R - L - 1 >= 0 ? R - L - 1 : 0;
    };
    console.log(maxDistance(nums1 = [55, 30, 5, 4, 2], nums2 = [100, 20, 10, 10, 5]));
  </script>
</body>

</html>